package com.century.sjt.adapter;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.century.sjt.R;
import com.century.sjt.entity.Commodit;
import com.century.sjt.waterfall.dodowaterfall.widget.ScaleImageView;
import com.century.sjt.waterfall.util.ImageFetcher;
import com.century.sjt.waterfall.view.XListView;

import java.util.List;

/**
 * Created by 炳基 on 2016/8/26.
 */
public class aQueryCommodityAdapter extends BaseAdapter {
    private Activity mContext;
    private List<Commodit> mInfos;
    private XListView mListView;
    private Handler mHandler;
    private ImageFetcher mImageFetcher;

    public aQueryCommodityAdapter(Activity context, XListView xListView, Handler mHandler, ImageFetcher mImageFetcher, List<Commodit> mInfos) {
        this.mContext = context;
        this.mInfos = mInfos;
        this.mListView = xListView;
        this.mHandler = mHandler;
        this.mImageFetcher = mImageFetcher;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        final Commodit duitangInfo = mInfos.get(position);

        if (convertView == null) {
            LayoutInflater layoutInflator = LayoutInflater.from(parent.getContext());
            convertView = layoutInflator.inflate(R.layout.infos_list, null);
            holder = new ViewHolder();
            holder.imageView = (ScaleImageView) convertView.findViewById(R.id.commodity_pic);
            holder.contentView = (TextView) convertView.findViewById(R.id.commodity_title);
            holder.tvPrice = (TextView) convertView.findViewById(R.id.commodity_price);
            holder.icon_select2 = (TextView) convertView.findViewById(R.id.icon_select2);
            holder.ll_selected = (LinearLayout) convertView.findViewById(R.id.ll_selected);
            Typeface iconfont = Typeface.createFromAsset(mContext.getAssets(), "iconfont.ttf");
            holder.icon_select2.setTypeface(iconfont);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.icon_select2.setVisibility(View.GONE);
//        if (mInfos.get(position).isSelect){
//            holder.icon_select2.setText(mContext.getResources().getString(R.string.icon_select2));
//        }else {
//            holder.icon_select2.setText(mContext.getResources().getString(R.string.icon_unselected));
//        }
//        holder.ll_selected.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (mInfos.get(position).isSelect){
//                    mInfos.get(position).setSelect(false);
//                    holder.icon_select2.setText(mContext.getResources().getString(R.string.icon_unselected));
//                }else {
//                    mInfos.get(position).setSelect(true);
//                    holder.icon_select2.setText(mContext.getResources().getString(R.string.icon_select2));
//                }
//
//                Message msg = new Message();
//                msg.what = 1;
//                mHandler.sendMessage(msg);
//
//            }
//        });
        holder.imageView.setImageWidth(duitangInfo.getWidth());
        holder.imageView.setImageHeight(duitangInfo.getHeight());
        Glide.with(mContext).load(duitangInfo.getMainImageUrl()).into(holder.imageView);
        holder.tvPrice.setText("￥"+ duitangInfo.getPrice());
        holder.contentView.setText(duitangInfo.getGoodName());
//        mImageFetcher.loadImage(duitangInfo.getIsrc(), holder.imageView);

        return convertView;
    }

    private static class ViewHolder {
        ScaleImageView imageView;
        TextView contentView;
        TextView tvPrice;
        TextView icon_select2;
        LinearLayout ll_selected;
    }

    @Override
    public int getCount() {
        return mInfos.size();
    }

    @Override
    public Object getItem(int arg0) {
        return mInfos.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return 0;
    }

    public void addItemLast(List<Commodit> datas) {
        mInfos.addAll(datas);

    }
    public void addItemTop(List<Commodit> datas) {
        mInfos.clear();
        mInfos.addAll(datas);
//        for (Commodit info : datas) {
//            mInfos.addFirst(info);
//        }
    }
}
